package com.sjgtw.cloud.web.rainfallMonitoring.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.constant.SjgtwConstont;
import com.sjgtw.cloud.web.rainfall.domain.RainfallWarning;
import com.sjgtw.cloud.web.rainfall.mapper.RainfallWarningMapper;
import com.sjgtw.cloud.web.rainfallGauge.domain.RainfallGauge;
import com.sjgtw.cloud.web.rainfallGauge.mapper.RainfallGaugeMapper;
import com.sjgtw.cloud.web.rainfallMonitoring.domain.FcRainfallValue;
import com.sjgtw.cloud.web.rainfallMonitoring.domain.RainfallMonitoring;
import com.sjgtw.cloud.web.rainfallMonitoring.mapper.FcRainfallValueMapper;
import com.sjgtw.cloud.web.rainfallMonitoring.mapper.RainfallMonitoringMapper;
import com.sjgtw.cloud.web.rainfallMonitoring.service.IRainfallMonitoringService;
import com.sjgtw.cloud.web.rainfallRange.domain.RainfallRange;
import com.sjgtw.cloud.web.rainfallRange.mapper.RainfallRangeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 雨量监测记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-17
 */
@Service
public class RainfallMonitoringServiceImpl implements IRainfallMonitoringService
{
    @Autowired
    private RainfallMonitoringMapper rainfallMonitoringMapper;

    @Autowired
    private RainfallGaugeMapper rainfallGaugeMapper;

    @Autowired
    private RainfallRangeMapper rainfallRangeMapper;

    @Autowired
    private RainfallWarningMapper rainfallWarningMapper;

    @Autowired
    private FcRainfallValueMapper fcRainfallValueMapper;

    @Value("${third.rainfall}")
    private String url;

    /**
     * 查询雨量监测记录
     * 
     * @param id 雨量监测记录主键
     * @return 雨量监测记录
     */
    @Override
    public RainfallMonitoring selectRainfallMonitoringById(Long id)
    {
        return rainfallMonitoringMapper.selectRainfallMonitoringById(id);
    }

    @Override
    public AjaxResult processRecord() {

        // 找fc_rainfall_value 表里面所有雨量计最新的一条数据，判断自己的库里面有没有这一条
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<FcRainfallValue> fcRainfallValueList = fcRainfallValueMapper.selectMaxTimeFcRainfallValueList(new FcRainfallValue());

        if(fcRainfallValueList.size()>0){
            for (int i = 0; i < fcRainfallValueList.size(); i++) {

                FcRainfallValue fcRainfallValue = fcRainfallValueList.get(i);
                if(fcRainfallValue == null){
                    continue;
                }
                Date dataTime = fcRainfallValue.getDataTime();

                try {

                    //System.out.println(sdf.format(dataTime));
                    RainfallMonitoring rainfallMonitoring1 = rainfallMonitoringMapper.selectRainfallMonitoringByCreateDate(sdf.parse(sdf.format(dataTime)));
                    //判断自己的库里面有没有这一条,有就不再次判断了
                    if(rainfallMonitoring1 != null){
                        System.out.println("该条记录已经被统计过。。。。。。");
                        continue;
                    }

                    // 得到数据后，判断雨量计所属区间，找到阈值，比较
                    RainfallGauge rainfallGauge = rainfallGaugeMapper.selectRainfallGaugeByKilometer(Double.valueOf(fcRainfallValue.getMileage()));

                    if(rainfallGauge != null){
                        RainfallRange rainfallRange = rainfallRangeMapper.selectRainfallRangeById(rainfallGauge.getRainfallRangeId());

                        boolean isTen = true;
                        boolean isHour = true;
                        boolean isDay = true;

                        RainfallMonitoring rainfallMonitoring = new RainfallMonitoring();
                        rainfallMonitoring.setTenMinute(fcRainfallValue.getRainfallPerTenMin());
                        rainfallMonitoring.setOneHour(fcRainfallValue.getRainfallPerHour());
                        rainfallMonitoring.setOneDay(fcRainfallValue.getRainfallPerDay());
                        rainfallMonitoring.setRainGaugeId(rainfallGauge.getId());
                        rainfallMonitoring.setIsValid(1);
                        rainfallMonitoring.setStatus(0);
                        rainfallMonitoring.setCreateTime(dataTime);


                        Double tenMinute = fcRainfallValue.getRainfallPerTenMin();
                        Double oneHour = fcRainfallValue.getRainfallPerHour();
                        Double oneDay = fcRainfallValue.getRainfallPerDay();

                        // 如果超过阈值，产生报警信息，结束判断。状态值能升级不能降级
                        //十分钟雨量判断
                        if(tenMinute != null){
                            if(tenMinute > rainfallRange.getWalkaboutTenMinute()){
                                rainfallMonitoring.setStatus(1);
                                isTen = false;
                            }
                        }
                        //一小时雨量判断
                        if(oneHour != null){
                            if(oneHour > rainfallRange.getBlockadeOneHour() && isHour ){
                                // 封锁雨量警戒值
                                rainfallMonitoring.setStatus(3);
                                isHour = false;
                            }else if(oneHour > rainfallRange.getSpeedLimitOneHour() && isHour){
                                rainfallMonitoring.setStatus(2);
                                isHour = false;
                            }else if(oneHour > rainfallRange.getWalkaboutOneHour() && isHour){
                                //
                                rainfallMonitoring.setStatus(1);
                                isHour = false;
                            }
                        }
                        //一天雨量判断
                        if(oneDay != null){
                            if(oneDay > rainfallRange.getBlockadeOneDay() && isDay ){
                                // 封锁雨量警戒值
                                rainfallMonitoring.setStatus(3);
                                isDay = false;

                            }else if(oneDay > rainfallRange.getSpeedLimitOneDay() && oneHour > rainfallRange.getSpeedLimitOneDayHour() && isDay){
                                if(rainfallMonitoring.getStatus() <= 2){
                                    rainfallMonitoring.setStatus(2);
                                    isDay = false;
                                }
                            }else if(oneDay > rainfallRange.getWalkaboutOneHour() && isDay){
                                //
                                if(rainfallMonitoring.getStatus() <= 1){
                                    rainfallMonitoring.setStatus(1);
                                    isDay = false;
                                }

                            }
                        }

                        rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);

                        if(!isTen || !isHour || !isDay){

                            RainfallWarning rainfallWarning = new RainfallWarning();

                            rainfallWarning.setWarningTime(new Date());
                            if(1 == rainfallMonitoring.getStatus()){
                                rainfallWarning.setWarningType("出巡预警");
                            }else if(2 == rainfallMonitoring.getStatus()){
                                rainfallWarning.setWarningType("限速预警");
                            }else if(3 == rainfallMonitoring.getStatus()){
                                rainfallWarning.setWarningType("封锁预警");
                            }
                            // 增加降雨巡视
                            rainfallWarning.setGaugeId(rainfallGauge.getId());
                            rainfallWarning.setLocation(rainfallGauge.getLocation());
                            rainfallWarning.setResponsibleUserId(rainfallGauge.getPersonId());
                            rainfallWarning.setTenMins(String.valueOf(tenMinute));
                            rainfallWarning.setOneDay(String.valueOf(oneDay));
                            rainfallWarning.setAnHour(String.valueOf(oneHour));
                            rainfallWarning.setStatus(SjgtwConstont.RainfallWarningStatus.notDispose.getCode());
                            rainfallWarningMapper.insertRainfallWarning(rainfallWarning);
                        }
                    }else{
                        System.out.println("本系统未找到该雨量计。。。。。。");
                        continue;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return AjaxResult.success("调用成功");
    }

    @Override
    public AjaxResult alarm(JSONObject jsonObject) {

      /*  Long rainfallGaugeId = jsonObject.getLong("rainfallGaugeId");

        Long tenMinute = jsonObject.getLong("tenMinute");
        Double oneHour = jsonObject.getDouble("oneHour") != null ?jsonObject.getDouble("oneHour"):0;
        Double oneDay = jsonObject.getDouble("oneDay");


        RainfallGauge rainfallGauge = rainfallGaugeMapper.selectRainfallGaugeById(rainfallGaugeId);


        RainfallRange rainfallRange = rainfallRangeMapper.selectRainfallRangeById(rainfallGauge.getRainfallRangeId());

        boolean isTen = true;
        boolean isHour = true;
        boolean isDay = true;

        RainfallMonitoring rainfallMonitoring = new RainfallMonitoring();
        rainfallMonitoring.setTenMinute(tenMinute);
        rainfallMonitoring.setOneHour(String.valueOf(oneHour));
        rainfallMonitoring.setOneDay(String.valueOf(oneDay));
        rainfallMonitoring.setRainGaugeId(rainfallGaugeId);
        rainfallMonitoring.setCreateTime(new Date());
        if(tenMinute != null){
            if(tenMinute > rainfallRange.getWalkaboutTenMinute()){
                rainfallMonitoring.setStatus(1);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isTen = false;
            }
        }
        if(oneHour != null){
            if(oneHour > rainfallRange.getBlockadeOneHour() && isHour ){
                // 封锁雨量警戒值
                rainfallMonitoring.setStatus(3);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isHour = false;
            }else if(oneHour > rainfallRange.getSpeedLimitOneHour() && isHour){
                rainfallMonitoring.setStatus(2);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isHour = false;
            }else if(oneHour > rainfallRange.getWalkaboutOneHour() && isHour){
                //
                rainfallMonitoring.setStatus(1);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isHour = false;
            }
        }

        if(oneDay != null){
            if(oneDay > rainfallRange.getBlockadeOneDay() && isDay ){
                // 封锁雨量警戒值
                rainfallMonitoring.setStatus(3);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isDay = false;
            }else if(oneDay > rainfallRange.getSpeedLimitOneDay() && oneHour > rainfallRange.getSpeedLimitOneDayHour() && isDay){
                rainfallMonitoring.setStatus(2);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isDay = false;
            }else if(oneDay > rainfallRange.getWalkaboutOneHour() && isDay){
                //
                rainfallMonitoring.setStatus(1);
                rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
                isDay = false;
            }
        }

        if(!isTen || !isHour || !isDay){

            RainfallWarning rainfallWarning = new RainfallWarning();

            rainfallWarning.setWarningTime(new Date());
            if(1 == rainfallMonitoring.getStatus()){
                rainfallWarning.setWarningType("出巡预警");
            }else if(2 == rainfallMonitoring.getStatus()){
                rainfallWarning.setWarningType("限速预警");
            }else if(3 == rainfallMonitoring.getStatus()){
                rainfallWarning.setWarningType("封锁预警");
            }

            rainfallWarning.setGaugeId(rainfallGaugeId);
            rainfallWarning.setLocation(rainfallGauge.getLocation());
            rainfallWarning.setResponsibleUserId(rainfallGauge.getPersonId());
            rainfallWarning.setTenMins(String.valueOf(tenMinute));
            rainfallWarning.setOneDay(String.valueOf(oneDay));
            rainfallWarning.setAnHour(String.valueOf(oneHour));
            rainfallWarning.setStatus(SjgtwConstont.RainfallWarningStatus.notDispose.getCode());
            rainfallWarningMapper.insertRainfallWarning(rainfallWarning);
        }
*/
        return AjaxResult.success("上报成功");
    }

    /**
     * 查询雨量监测记录列表
     * 
     * @param rainfallMonitoring 雨量监测记录
     * @return 雨量监测记录
     */
      @Override
    public  List<Map<String,Object>> selectRainfallMonitoringList(RainfallMonitoring rainfallMonitoring)
    {
        return rainfallMonitoringMapper.selectRainfallMonitoringList(rainfallMonitoring);
    }

    @Override
    public  List<Map<String,Object>> getRainfallMonitoringList(RainfallMonitoring rainfallMonitoring)
    {
        List<Map<String, Object>> maps = rainfallGaugeMapper.selectRainfallGaugeList(new RainfallGauge());

        if(maps.size()>0){
            for (Map<String, Object> map : maps) {
                Long id = (Long) map.get("id");
                RainfallMonitoring rainGaugeMaxTimeData = rainfallMonitoringMapper.getRainGaugeMaxTimeData(id);
                if(rainGaugeMaxTimeData !=null){
                    map.put("tenMinute",rainGaugeMaxTimeData.getTenMinute());
                    map.put("oneHour",rainGaugeMaxTimeData.getOneHour());
                    map.put("oneDay",rainGaugeMaxTimeData.getOneDay());
                    map.put("status",rainGaugeMaxTimeData.getStatus());
                    map.put("createTime",rainGaugeMaxTimeData.getCreateTime());
                }else{
                    map.put("tenMinute",null);
                    map.put("oneHour",null);
                    map.put("oneDay",null);
                    map.put("status",null);
                    map.put("createTime",null);
                }

                StringBuilder sb = new StringBuilder();
                Double kilometer = (Double)map.get("kilometer");
                if(kilometer != null){
                    String[] split = String.valueOf(kilometer).split("\\.");
                    if(split.length==2){
                        if(StringUtils.isNotEmpty(split[0])){
                            sb.append("K");
                            sb.append(split[0]);
                        }
                        if(StringUtils.isNotEmpty(split[1])){
                            sb.append("+");
                            sb.append(split[1]);
                        }
                    }else if(split.length ==1){
                        if(StringUtils.isNotEmpty(split[0])){
                            sb.append("K");
                            sb.append(split[0]);
                        }
                    }
                }
                map.put("kilometer",sb.toString());
            }
        }

        return maps;
    }





    /**
     * 新增雨量监测记录
     * 
     * @param rainfallMonitoring 雨量监测记录
     * @return 结果
     */
    @Override
    public int insertRainfallMonitoring(RainfallMonitoring rainfallMonitoring)
    {
        rainfallMonitoring.setCreateTime(DateUtils.getNowDate());
        return rainfallMonitoringMapper.insertRainfallMonitoring(rainfallMonitoring);
    }

    /**
     * 修改雨量监测记录
     * 
     * @param rainfallMonitoring 雨量监测记录
     * @return 结果
     */
    @Override
    public int updateRainfallMonitoring(RainfallMonitoring rainfallMonitoring)
    {
        return rainfallMonitoringMapper.updateRainfallMonitoring(rainfallMonitoring);
    }

    /**
     * 批量删除雨量监测记录
     * 
     * @param ids 需要删除的雨量监测记录主键
     * @return 结果
     */
    @Override
    public int deleteRainfallMonitoringByIds(Long[] ids)
    {
        return rainfallMonitoringMapper.deleteRainfallMonitoringByIds(ids);
    }

    /**
     * 删除雨量监测记录信息
     * 
     * @param id 雨量监测记录主键
     * @return 结果
     */
    @Override
    public int deleteRainfallMonitoringById(Long id)
    {
        return rainfallMonitoringMapper.deleteRainfallMonitoringById(id);
    }
}
